using System;
using System.Collections;
using System.Data.SqlClient;
using System.Text;
using System.Data;

namespace GenerationCode .Utils
{
    class Server
    {
        #region fields
		private const string QUERY = "SELECT name FROM sysdatabases WHERE dbid > 4";

        private string name;
        private string userId;
        private string password;
		private string connectionString;		
        #endregion 

        #region constructor
        public Server (string name, string userId, string password)
        {
            this.name = name;
            this.userId = userId;
            this.password = password;
			this.connectionString = string.Format( @"server={0};database=master;uid={1};pwd={2};",
															this.name, this.userId, this.password );
        }
        #endregion

        #region properties
        public string Name
        {
            get
            {
                return this.name;
            }
        }
        public string UserId
        {
            get
            {
                return this.userId;
            }
        }
        public string Password
        {
           get
           {
               return this.password;
           }
        }
		public IList Databases {
			get {
				return this.GetDatabases();
			}
		}
        #endregion 

		#region method
		private IList GetDatabases () {
			IList lst = new ArrayList();
			DataTable tb = null;
			try {
				tb = Utility.GetObjects( this.connectionString, QUERY );
				foreach ( DataRow row in tb.Rows ) {
					string dbName = (string) row[0];
					string connectStr = string.Format( @"server={0};database={1};uid={2};pwd={3};", 
											this.name, dbName, this.userId, this.password );
					Database db = new Database( dbName, connectStr );
					lst.Add( db );
				}				
			} catch ( Exception ex ) {
				throw ex;								
			}						
			return lst;
		}
		#endregion 

    }
}
